package com.wrench.elevator.base.user.service;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import com.wrench.elevator.base.user.dao.AddressDao;
import com.wrench.elevator.base.user.domain.Address;
import com.wrench.elevator.constant.Dict;
import com.wrench.elevator.constant.Result;
import com.wrench.elevator.exception.WrenchException;
import com.wrench.elevator.util.WrenchUtil;

/**
 * @Description: AddressService
 * @author yangchenfeng
 * @date Oct 24, 2017 3:48:48 PM
 * @version V1.0
 */
@Component
public class AddressService
{
    @Resource
    private AddressDao dao;
    
    private static final Logger logger = LoggerFactory.getLogger(AddressService.class);
    
    public int setAddress(String user_id, String province_id, String province_name, String city_id, String city_name,
        String district_id, String district_name, String street_name, String building_id, String building_name,
        String zhuangshu, String danyuan, String loucen, String menpaihao)
        throws WrenchException
    {
        if (WrenchUtil.hasNull(user_id,
            province_id,
            province_name,
            city_id,
            city_name,
            district_id,
            district_name,
            street_name,
            building_id,
            building_name,
            zhuangshu,
            danyuan,
            loucen,
            menpaihao))
        {
            logger.error("==> param is not found , user_id = {} , province_id = {} , province_name = {} , city_id = {} , city_name = {} , district_id = {} , district_name = {} , street_name = {} , building_id = {} , building_name = {} , zhuangshu = {} , danyuan = {} , loucen = {} , menpaihao = {} ",
                user_id,
                province_id,
                province_name,
                city_id,
                city_name,
                district_id,
                district_name,
                street_name,
                building_id,
                building_name,
                zhuangshu,
                danyuan,
                loucen,
                menpaihao);
            throw new WrenchException(Result.PARAM_NOT_FOUND_CODE, Result.PARAM_NOT_FOUND_MESSAGE);
        }
        
        user_id = user_id.trim();
        province_id = province_id.trim();
        province_name = province_name.trim();
        city_id = city_id.trim();
        city_name = city_name.trim();
        district_id = district_id.trim();
        district_name = district_name.trim();
        street_name = street_name.trim();
        building_id = building_id.trim();
        building_name = building_name.trim();
        zhuangshu = zhuangshu.trim();
        danyuan = danyuan.trim();
        loucen = loucen.trim();
        menpaihao = menpaihao.trim();
        
        // 判 空格
        if (WrenchUtil.hasBlank(user_id,
            province_id,
            province_name,
            city_id,
            city_name,
            district_id,
            district_name,
            street_name,
            building_id,
            building_name,
            zhuangshu,
            danyuan,
            loucen,
            menpaihao))
        {
            logger.error("==> string has blank , user_id = {} , province_id = {} , province_name = {} , city_id = {} , city_name = {} , district_id = {} , district_name = {} , street_name = {} , building_id = {} , building_name = {} , zhuangshu = {} , danyuan = {} , loucen = {} , menpaihao = {} ",
                user_id,
                province_id,
                province_name,
                city_id,
                city_name,
                district_id,
                district_name,
                street_name,
                building_id,
                building_name,
                zhuangshu,
                danyuan,
                loucen,
                menpaihao);
            throw new WrenchException(Result.PARAM_NOT_FOUND_CODE, Result.PARAM_NOT_FOUND_MESSAGE);
        }
        
        // 判整数
        if (!WrenchUtil.areInt(user_id, province_id, city_id, district_id, building_id, loucen))
        {
            logger.error("==> string is not int , user_id = {} , province_id = {} , city_id = {} , district_id = {} , building_id = {} , loucen = {} ",
                user_id,
                province_id,
                city_id,
                district_id,
                building_id,
                loucen);
            throw new WrenchException(Result.STRING_IS_NOT_INT_CODE, Result.STRING_IS_NOT_INT_MESSAGE);
        }
        
        Address address = new Address();
        address.setUser_id(Integer.valueOf(user_id));
        address.setProvince_id(Byte.valueOf(province_id));
        address.setProvince_name(province_name);
        address.setCity_id(Short.valueOf(city_id));
        address.setCity_name(city_name);
        address.setDistrict_id(Short.valueOf(district_id));
        address.setDistrict_name(district_name);
        address.setStreet_name(street_name);
        address.setBuilding_id(Integer.valueOf(building_id));
        address.setBuilding_name(building_name);
        address.setZhuangshu(zhuangshu);
        address.setDanyuan(danyuan);
        address.setLoucen(Byte.valueOf(loucen));
        address.setMenpaihao(menpaihao);
        address.setAddress(WrenchUtil.concat(province_name,
            city_name,
            district_name,
            street_name,
            building_name,
            zhuangshu,
            danyuan,
            loucen,
            "层",
            menpaihao));
        address.setInput_time(WrenchUtil.nowTime());
        address.setAddress_status(Dict.ADDRESS_ADDRESS_STATUS_USING);
        
        // 设置地址
        int commit = -1;
        
        try
        {
            commit = dao.setAddress(address);
        }
        catch (Exception e)
        {
            logger.error("==> commit=dao.setAddress(address) execute error , address = {} , error = {}; ", address, e);
            throw new WrenchException(Result.DB_INSERT_ERROR_CODE, Result.DB_INSERT_ERROR_MESSAGE);
        }
        
        if (commit <= 0)
        {
            logger.error("==> commit=dao.setAddress(address) execute success , but commit <=0 , commit = {} , address = {} ",
                commit,
                address);
            throw new WrenchException(Result.ADDRESS_SET_ADDRESS_FAIL_CODE, Result.ADDRESS_SET_ADDRESS_FAIL_MESSAGE);
        }
        
        logger.debug("==> commit=dao.setAddress(address) execute success , address = {} , commit = {} ",
            address,
            commit);
        
        return commit;
        
    }
    
    public int setOldAddressStatus(String user_id, String address_status)
        throws WrenchException
    {
        // 判空
        if (WrenchUtil.hasNull(user_id, address_status))
        {
            logger.error("==> param is not found , user_id = {} , address_status = {} ", user_id, address_status);
            throw new WrenchException(Result.PARAM_NOT_FOUND_CODE, Result.PARAM_NOT_FOUND_MESSAGE);
            
        }
        
        // 地址状态
        if (!WrenchUtil.isVaildAddressStatus(address_status))
        {
            logger.error("==> address status is not vaild , address_status = {} ", address_status);
            throw new WrenchException(Result.ADDRESS_ADDRESS_STATUS_IS_NOT_VAILD_CODE,
                Result.ADDRESS_ADDRESS_STATUS_IS_NOT_VAILD_MESSAGE);
        }
        
        // 判断是否整数
        if (!WrenchUtil.isInt(user_id))
        {
            logger.error("==> string is not int , user_id = {} ", user_id);
            throw new WrenchException(Result.STRING_IS_NOT_INT_CODE, Result.STRING_IS_NOT_INT_MESSAGE);
        }
        
        int commit = -1;
        try
        {
            commit = dao.setOldAddressStatus(Integer.valueOf(user_id), address_status);
        }
        catch (Exception e)
        {
            logger.error("==> commit=dao.setOldAddressStatus(Integer.valueOf(user_id), address_status) execute error , user_id = {} , address_status = {} , error = {}; ",
                user_id,
                address_status,
                e);
            throw new WrenchException(Result.DB_UPDATE_ERROR_CODE, Result.DB_UPDATE_ERROR_MESSAGE);
        }
        
//        if (commit <= 0)
//        {
//            logger.error("==> commit=dao.setOldAddressStatus(Integer.valueOf(user_id), address_status) execute success , but commit <=0 , commit = {} , user_id = {} , address_status = {} ",
//                commit,
//                user_id,
//                address_status);
//            throw new WrenchException(Result.ADDRESS_SET_OLD_ADDRESS_STATUS_FAIL_CODE,
//                Result.ADDRESS_SET_OLD_ADDRESS_STATUS_FAIL_MESSAGE);
//        }
        
        logger.debug("==> commit=dao.setOldAddressStatus(Integer.valueOf(user_id), address_status) execute success , user_id = {} , address_status = {} , commit = {} ",
            user_id,
            address_status,
            commit);
        
        return commit;
        
    }
}
